# _*_ coding: UTF-8 _*_
# @Time     : 2021/11/15 13:40
# @Author   : 郑鲁明
# @Site     : http://www.hzdledu.cn/
# @File     : prac.py
# @Software : PyCharm

import unittest
from appium import webdriver
import time


class Test1(unittest.TestCase):
    def setUp(self) -> None:
        # 手机设置
        desired_caps = {}
        desired_caps['platformName'] = 'Android'  # 系统名称
        desired_caps['platformVersion'] = '7.1.2'  # 系统的版本号
        desired_caps['deviceName'] = 'Android Emulator'  # 设备名称，这里是虚拟机，这个没有严格的规定
        desired_caps['appPackage'] = 'com.netease.cloudmusic'  # APP包名
        desired_caps['appActivity'] = 'com.netease.cloudmusic.activity.LoadingActivity'  # APP入口的activity
        desired_caps['noReset'] = True  # 不重置app的缓存文件
        desired_caps['unicodeKeyboard'] = True  # 设置键盘支持中文输入
        desired_caps['resetKeyboard'] = True  # 重置键盘

        # 连接appium server，告诉appium，代码要操作哪个设备上的哪个APP
        # 启动手机上App
        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

    def test1(self):
        # 实现网易云音乐的登录，并验证，搜索一首歌，播放30秒，添加评论
        # 点击手机号登录
        time.sleep(1)
        self.driver.find_element_by_id('com.netease.cloudmusic:id/f8').click()
        # 输入手机号
        time.sleep(1)
        self.driver.find_element_by_id('com.netease.cloudmusic:id/a69').send_keys('18758398146')
        # 输入密码
        time.sleep(1)
        self.driver.find_element_by_id('com.netease.cloudmusic:id/f7').send_keys('aini1314..')
        # 点击登录
        time.sleep(1)
        self.driver.find_element_by_id('com.netease.cloudmusic:id/f8').click()
        # 验证登录

        # 点击搜索
        time.sleep(1)
        self.driver.find_element_by_accessibility_id('搜索').click()  # 点击搜索图标
        # 清空输入框
        time.sleep(1)
        self.driver.find_element_by_id('com.netease.cloudmusic:id/search_src_text').clear()
        # 输入
        time.sleep(1)
        self.driver.find_element_by_id('com.netease.cloudmusic:id/search_src_text').send_keys('漠河舞厅')
        # 基于坐标操作
        # 点击搜索栏的第一项
        time.sleep(1)
        self.driver.tap([(128, 226)])
        # 选择第一项
        time.sleep(1)
        self.driver.tap([(80,200)])
        # 播放30秒
        time.sleep(1)
        self.driver.find_element_by_id('com.netease.cloudmusic:id/o8').click()
        # 播放30秒
        time.sleep(10)
        # 点击评论
        time.sleep(1)
        self.driver.find_element_by_id('com.netease.cloudmusic:id/o0').click()
        # 输入评论
        time.sleep(2)
        self.driver.find_element_by_id('com.netease.cloudmusic:id/h2').send_keys('很好听')
        # 点击发送
        time.sleep(1)
        self.driver.find_element_by_id('com.netease.cloudmusic:id/a2a').click()



        time.sleep(5)

    def tearDown(self) -> None:
        self.driver.quit()